<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-card class="box-card" shadow="hover">
      <div slot="header" class="clearfix">
        <span class="card-title">查询条件</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="showSearch = !showSearch">
          {{ showSearch ? '收起' : '展开' }}
        </el-button>
      </div>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="姓名" prop="xm">
              <el-input
                v-model="queryParams.xm"
                placeholder="请输入姓名"
                clearable
                prefix-icon="el-icon-user"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="性别" prop="xb">
              <el-select v-model="queryParams.xb" placeholder="请选择性别" clearable style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="教师所属部门" prop="jsSsBm">
              <el-input
                v-model="queryParams.jsSsBm"
                placeholder="请输入教师所属部门"
                clearable
                prefix-icon="el-icon-office-building"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="教师职称" prop="jsZc">
              <el-select v-model="queryParams.jsZc" placeholder="请选择教师职称" clearable style="width: 100%">
                <el-option
                  v-for="dict in dict.type.js_zc"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" style="text-align: right">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <!-- 操作按钮区域 -->
    <el-card class="box-card" shadow="hover" style="margin-top: 15px">
      <div class="table-operations">
        <el-button-group>
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="handleAdd"
            v-hasPermi="['jxgl:jsgl:add']"
          >新增</el-button>
          <el-button
            type="success"
            icon="el-icon-edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['jxgl:jsgl:edit']"
          >修改</el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['jxgl:jsgl:remove']"
          >删除</el-button>
          <el-button
            type="warning"
            icon="el-icon-download"
            @click="handleExport"
            v-hasPermi="['jxgl:jsgl:export']"
          >导出</el-button>
        </el-button-group>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </div>

      <!-- 表格区域 -->
      <el-table
        v-loading="loading"
        :data="jsglList"
        @selection-change="handleSelectionChange"
        border
        stripe
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column type="index" label="序号" width="80" align="center" />
        <el-table-column label="姓名" align="center" prop="xm" />
        <el-table-column label="性别" align="center" prop="xb">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.xb"/>
          </template>
        </el-table-column>
        <el-table-column label="教师所属部门ID" align="center" prop="jsSsBmId" />
        <el-table-column label="教师所属部门" align="center" prop="jsSsBm" />
        <el-table-column label="联系电话" align="center" prop="lxDh" />
        <el-table-column label="教师职称" align="center" prop="jsZc" />
        <el-table-column label="操作" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['jxgl:jsgl:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['jxgl:jsgl:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改教师管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="姓名" prop="xm">
              <el-input v-model="form.xm" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="xb">
              <el-select v-model="form.xb" placeholder="请选择性别" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="教师所属部门" prop="jsSsBmId">
              <treeselect
                v-model="form.jsSsBmId"
                :options="deptOptions"
                :normalizer="normalizer"
                placeholder="请选择所属部门"
                @input="handleDeptChange"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="部门ID" prop="jsSsBmId">
              <el-input v-model="form.jsSsBmId" disabled placeholder="自动生成" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="联系电话" prop="lxDh">
              <el-input v-model="form.lxDh" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="教师职称" prop="jsZc">
              <el-select v-model="form.jsZc" placeholder="请选择教师职称" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.js_zc"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.app-container {
  padding: 10px;

  .box-card {
    margin-bottom: 10px !important;
    .card-title {
      font-size: 14px;
      font-weight: bold;
      color: #303133;
    }
  }

  .table-operations {
    margin-bottom: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .el-button-group {
    .el-button {
      margin-right: 0;
    }
  }

  .el-table {
    margin: 8px 0;
  }

  .el-form {
    .el-form-item {
      margin-bottom: 12px;
    }
  }

  .el-dialog {
    .el-dialog__body {
      padding: 16px 20px;
    }
  }

  .el-form-item__label {
    font-size: 12px !important;
  }
}
</style>

<script>
import { listJsgl, getJsgl, delJsgl, addJsgl, updateJsgl } from "@/api/jxgl/jsgl"
import Treeselect from "@riophae/vue-treeselect"
import "@riophae/vue-treeselect/dist/vue-treeselect.css"
import { listDept } from "@/api/system/dept"

export default {
  name: "Jsgl",
  dicts: ['js_zc', 'sys_user_sex'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教师管理表格数据
      jsglList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        xm: null,
        xb: null,
        jsSsBmId: null,
        jsSsBm: null,
        jsZc: null,
        userId: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        xm: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        xb: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
        jsSsBm: [
          { required: true, message: "教师所属部门不能为空", trigger: "blur" }
        ],
        lxDh: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        jsZc: [
          { required: true, message: "教师职称不能为空", trigger: "change" }
        ],
      },
      deptOptions: [],
    }
  },
  created() {
    this.getList()
    this.getDeptTree()
  },
  methods: {
    /** 查询教师管理列表 */
    getList() {
      this.loading = true
      listJsgl(this.queryParams).then(response => {
        this.jsglList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        jsglId: null,
        xm: null,
        xb: null,
        jsSsBmId: null,
        jsSsBm: null,
        lxDh: null,
        jsZc: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        deptId: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.jsglId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加教师管理"
      // 自动赋值部门名称
      if (this.form.jsSsBmId) {
        this.handleDeptChange(this.form.jsSsBmId)
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const jsglId = row && row.jsglId ? row.jsglId : this.ids
      getJsgl(jsglId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改教师管理"
        // 自动赋值部门名称
        if (this.form.jsSsBmId) {
          this.handleDeptChange(this.form.jsSsBmId)
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      // 自动赋值一次，防止遗漏
      if (this.form.jsSsBmId && !this.form.jsSsBm) {
        this.handleDeptChange(this.form.jsSsBmId)
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.jsglId != null) {
            updateJsgl(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addJsgl(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const jsglIds = row && row.jsglId ? row.jsglId : this.ids
      this.$modal.confirm('是否确认删除教师管理编号为"' + jsglIds + '"的数据项？').then(function() {
        return delJsgl(jsglIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jxgl/jsgl/export', {
        ...this.queryParams
      }, `jsgl_${new Date().getTime()}.xlsx`)
    },
    getDeptTree() {
      listDept().then(res => {
        this.deptOptions = res.data
      })
    },
    handleDeptChange(deptId) {
      // 递归查找部门名称
      const findDeptName = (options, id) => {
        for (const item of options) {
          if (item.deptId === id) return item.deptName
          if (item.children) {
            const res = findDeptName(item.children, id)
            if (res) return res
          }
        }
        return ''
      }
      this.form.jsSsBm = findDeptName(this.deptOptions, deptId) || ''
    },
    normalizer(node) {
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      }
    },
  }
}
</script>
